/**
 * 用户日志管理sysUserLog.js
 * 版本号： v1.0.0
 */
$(function () {

    //设置时间
    var curr_time = new Date();
    $("#startTime").datetimebox("setValue", getFirstDay(curr_time) + " 00:00:00");
    $("#endTime").datetimebox("setValue", getFirstDay(curr_time) + " 23:59:59");
    var beginTime = $("#startTime").datebox("getValue");
    var endTime = $("#endTime").datebox("getValue");


    //用户日志用户
    bindCombo_userDist("userId");

    /**
     * 用户日志列表
     */
    var lastChecked = null;
    $("#sysUserLog_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        queryParams:{startTime:beginTime, endTime:endTime},
        singleSelect: true,
        pagination: true,
        border: false,
        striped:true,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/sysUserLog/sysUserLogList.do',
        idField: 'uslId',
        frozenColumns: [[
            {field: 'uslId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'operatName', title: '登录类型', width: 60, align: 'center',fixed: true},
                {field: 'reqUrl', title: '请求地址', width: 230, fixed: true},
                {field: 'userIp', title: '请求IP', width: 120, fixed: true},
                {field: 'userAgent', title: '用户代理', width: 280},
                {field: '_userName', title: '操作人', width: 80, fixed: true,
                    formatter:function (val,row) {
                        return row.user.loginName;
                    }
                },
                {field: 'createDate', title: '操作时间', width: 135, fixed: true },
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#sysUserLog_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#sysUserLog_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#sysUserLog_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#sysUserLog_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            sysUserLog_loadSearch();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            sysUserLog_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查看用户日志详情
 */
function sysUserLog_view(iconCls){
    var checked = $("#sysUserLog_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要查看的日志", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#sysUserLog_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(dlrzxq.id, dlrzxq.url + row.uslId, iconCls, dlrzxq.closable, dlrzxq.title);
}


/**
 * 执行查询
 */
function sysUserLog_loadSearch() {
    //刷新dg页码
    refreshPageNumber("sysUserLog_dataList");
    $("#sysUserLog_dataList").datagrid("reload", {
        userId: $("#userId").combobox("getValue"),
        uslCode: $("#uslCode").combobox("getValue"),
        startTime: $("#startTime").datetimebox("getValue"),
        endTime: $("#endTime").datetimebox("getValue")
    });
    $("#sysUserLog_dataList").datagrid("clearSelections");
}


/**
 * 创建人列表
 * @param userControl
 */
function bindCombo_userDist(userControl) {
    $.ajax({
        url: CONTEXT_PATH + "/sysUserLog/getUserList.do",
        type: "get",
        success: function (data) {
            var combo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                combo.push({"text": data[i].userName, "value": data[i].userId});
            }
            $("#" + userControl).combobox("loadData", combo);
        }
    })
}


